package main

/*
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
*/
func swaqPairs(head *ListNode) *ListNode {
	if head == nil || head.next == nil {
		return head
	}
	s := head.next
	var behind *ListNode
	for head.next != nil {
		heaNext := head.next
		if behind != nil && behind.next != nil {
			behind.next = head.next
		}
		var next *ListNode
		if head.next.next != nil {
			head.next = next
		} else {
			head.next = nil
		}
		heaNext.next = head
		behind = head
		if head.next != nil {
			head = next
		}
	}
	return s
}
